﻿<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="UTF-8"> 
	<script src="FileSaver.min.js"></script>
	<!-- or https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.min.js -->
	<script src="openpgp.min.js"></script>
	<script>
	function createkeypair(){
		var openpgp = window.openpgp;
		var passphrase = prompt('准备创建密钥，请输入密钥口令');
		var name = prompt('请输入姓名');
		var id = prompt('请输入ID');
		var email = prompt('请输入Email');
		var userIDs = name + " (" + id + ") <" + email + ">" ;
		var publicKey,privateKey;
		var opt = {numBits: 4096, userIds: userIDs, passphrase: passphrase};
		openpgp.generateKey(opt).then(function(key) {
			alert(key.key.primaryKey.fingerprint+ "已创建成功。");
			var pub = new Blob([key.publicKeyArmored], {type: "text/plain;charset=utf-8"});
			alert("Save the Publc Key");
			saveAs(pub, "key.pub");
			var sec = new Blob([key.privateKeyArmored], {type: "text/plain;charset=utf-8"});
			alert("Save the Private Key");
			saveAs(sec, "key.sec");
			
			
		});
	}
	
	function ReadPubKey(files) {
		if (files.length) {
			var file = files[0];
			var reader = new FileReader();
			reader.onload = function() {
				var openpgp = window.openpgp;
				
				var publicKey = openpgp.key.readArmored(this.result).keys[0];
				var plaintext = prompt("准备加密，请输入明文");

				options = {
					data: plaintext,
					publicKeys: publicKey,  // for encryption
				};
				openpgp.encrypt(options).then(function(ciphertext) {
				pgpMessage = ciphertext.data; // '-----BEGIN PGP MESSAGE ... END PGP MESSAGE-----'
					// success
					document.write("密文：<br/><pre>"+pgpMessage+"</pre><br/><br/>");
				}).catch(function(error) {
					// failure
					alert("加密失败！");
				});		
			}
			reader.readAsText(file);
		}
	}
	
	function ReadSecKey(files) {
		if (files.length) {
			var file = files[0];
			var reader = new FileReader();
			reader.onload = function() {
				var openpgp = window.openpgp;
				
				var privateKey = openpgp.key.readArmored(this.result).keys[0];
				var plaintext = prompt("准备签名，请输入明文");
				var passphrase = prompt("准备签名，请输入密钥口令");
				
				if(privateKey.decrypt(passphrase)){
					alert("decrypted！");

					options = {
						data: plaintext,
						privateKeys: privateKey,
						detached: true
					};

					openpgp.sign(options).then(function(signed) {
						cleartext = signed.data;
						detachedSig = signed.signature;
						document.write("原文：<br/><pre>"+cleartext+"</pre><br/><br/><hr/>"+"签名：<br/><pre>"+detachedSig+"</pre><br/><br/>");
						
						alert(openpgp.cleartext.readArmored(cleartext).getText());
					});
				}
			}
			reader.readAsText(file);
		}
	}
	
	</script>
  </head>
  <body>
    <input type="button" id="createkey" value="创建密钥" onclick="createkeypair()"></input>
	<br/>
	<label>读取公钥文件</label><br/>
	<input type="file" id="files" value="读取公钥文件" multiple="" onchange="ReadPubKey(this.files)"/>
	<br/>
	<label>读取私钥文件</label><br/>
	<input type="file" id="files" value="读取私钥文件" multiple="" onchange="ReadSecKey(this.files)"/>
	
  
  </body>
</html>